/*
 * DeviceSearcherAgent.java
 *
 * Created on 7 novembre 2007, 15.52
 *
 * @version 1.1
 * @author Federico Paparoni
 *
 */

package com.javastaff.btfree;

import java.util.Vector;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

public class DeviceSearcherAgent implements DiscoveryListener{

    private LocalDevice localDevice;

    private DiscoveryAgent agent;
    
    private DeviceSearcher deviceSearcher;
    
    private Vector devicesFound;
    
    /** Creates a new instance of DeviceSearcherAgent */
    public DeviceSearcherAgent(DeviceSearcher deviceSearcher) {
        try {
            this.deviceSearcher=deviceSearcher;
            this.devicesFound=new Vector();
            localDevice = LocalDevice.getLocalDevice();
            agent = localDevice.getDiscoveryAgent();
            agent.startInquiry(DiscoveryAgent.GIAC,this);
        } catch (BluetoothStateException ex) {
            deviceSearcher.signalError(ex.toString());
        }
    }

    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
        devicesFound.addElement(remoteDevice);
    }

    public void servicesDiscovered(int i, ServiceRecord[] serviceRecord) {
    }

    public void serviceSearchCompleted(int i, int i0) {
    }

    public void inquiryCompleted(int i) {
        deviceSearcher.devicesFound(devicesFound);
    }
    
}
